package com.util.moduleManager 
{
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.utils.Dictionary;
	/**
	 * 并行加载资源
	 * @author henrylee {henrylee_sz@qq.com}
	 */
	public final class ParallelModule
	{
		private var _loader:Loader = null;
		private var _context:LoaderContext = null;
		private var _callBack:Function = null;
		private var _funObj:Object = null;
		private var _tepPaths:Array = null;
		
		private var _reloadCount: Dictionary;            //用来标记单个资源加载的次数
		private var _nLoader: int;                       //需要加载的资源数量
		private var _completeNum:int = 0;
		
		public function ParallelModule(paths:*, ...rest) 
		{
			if(rest[0])
				_callBack = rest[0];
			if (rest[1])
				_funObj = rest[1];
			
			if (paths is Array)
				_tepPaths = paths;
			else
				_tepPaths = [paths];
			
			_reloadCount = new Dictionary();
			for (var i:int = 0; i < _tepPaths.length; i++) 
			{
				_reloadCount[_tepPaths[i]] = [0, null];
			}
			
			_context = new LoaderContext();
			_context.applicationDomain = ApplicationDomain.currentDomain;
			
			_nLoader = _tepPaths.length;
			for (i = 0; i < 5; i++)
			{
				if (_tepPaths.length <= 0) break;
				loadResourceHandle(_tepPaths.shift());
			}
		}
		
		private function loadResourceHandle(path: String):void
		{
			if (_reloadCount[path][1] == null)
				_reloadCount[path][1] = new Loader();
			_loader = _reloadCount[path][1];
			_loader.load(new URLRequest(path), _context);
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
			//_loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
			_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			_loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
		}
		
		protected function onCompleteHandler(event:Event):void
		{
			event.currentTarget.removeEventListener(Event.COMPLETE, onCompleteHandler);
			//event.currentTarget.removeEventListener(ProgressEvent.PROGRESS, onProgressHandler);
			event.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			event.currentTarget.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
			
			_completeNum ++;
			if (_tepPaths.length > 0)
				loadResourceHandle(_tepPaths.shift());
			
			//需要的资源全部加载完成
			if (_completeNum >= _nLoader)
			{
				if (_callBack != null)
				{
					_callBack.apply(_funObj);
					remove();
				}
			}
		}
		
		//protected function onProgressHandler(event:ProgressEvent):void
		//{
			//var num:int = event.bytesLoaded * 100 / event.bytesTotal;
			//ModuleManager.getInstance().dispatchEvent(new Event(ModuleManager.PROGRESSING));
			//EventCenter.GameEvent.dispatchEvent(new GameEvent(GameEvent.LOADMODULE_PROGRESS, num));
		//}
		
		private function ioErrorHandler(event:IOErrorEvent):void
		{
			event.currentTarget.removeEventListener(Event.COMPLETE, onCompleteHandler);
			//event.currentTarget.removeEventListener(ProgressEvent.PROGRESS, onProgressHandler);
			event.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			event.currentTarget.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
			
			var __path: String = getPathByLoaderInfo(event.currentTarget as LoaderInfo);
			if (_reloadCount[__path][0] > 3 || __path == null)
			{
				//Remoting.instance.gameLog("Loading IOErrorEvent::" + __path);
				//Debug.debug(__path);
			}
			else
			{
				loadResourceHandle(__path);
			}
		}
		
		protected function securityErrorHandler(event:SecurityErrorEvent):void
		{
			event.currentTarget.removeEventListener(Event.COMPLETE, onCompleteHandler);
			//event.currentTarget.removeEventListener(ProgressEvent.PROGRESS, onProgressHandler);
			event.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			event.currentTarget.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
			
			var __path: String = getPathByLoaderInfo(event.currentTarget as LoaderInfo);
			if (_reloadCount[__path][0] > 3 || __path == null)
			{
				//Remoting.instance.gameLog("Loading SecurityErrorEvent::" + __path);
				//Debug.debug(__path);
			}
			else
			{
				loadResourceHandle(__path);
			}
		}
		
		/**
		 * 获得资源路径
		 * 由于加载失败的时候loaderInfo里的url是null，所以增加这个获得资源路径的方式
		 * @param	loaderInfo
		 * @return
		 */
		private function getPathByLoaderInfo(infos: LoaderInfo):String
		{
			for (var key: String in _reloadCount)
			{
				if (_reloadCount[key][1] != null)
				{
					if (Loader(_reloadCount[key][1]).contentLoaderInfo == infos)
						return key;
				}
			}
			return null;
		}
		
		
		private function remove():void
		{
			for (var key: String in _reloadCount)
				delete _reloadCount[key];
			
			_reloadCount = null;
			_loader = null;
			_callBack = null;
			_funObj = null;
			_tepPaths = null;
		}
		
	}

}